#!/bin/sh

# Slackware build script for gpsman
# Written by Kyle Guinn <elyk03@gmail.com>

set -e

PRGNAM=gpsman
VERSION=6.3.2
ARCH=noarch
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar -xzvf $CWD/$PRGNAM-$VERSION.tgz
cd $PRGNAM-$VERSION
chown -R root:root .
find . -type f -exec chmod 0644 {} \;
find . -type d -exec chmod 0755 {} \;

mkdir -p $PKG/usr/bin
mkdir -p $PKG/usr/lib/$PRGNAM
mkdir -p $PKG/usr/man/man1
mkdir -p $PKG/usr/share/$PRGNAM

sed -e "s/set SRCDIR gmsrc/set SRCDIR \/usr\/lib\/gpsman/" ./gpsman.tcl > $PKG/usr/lib/$PRGNAM/gpsman.tcl

chmod 0755 $PKG/usr/lib/$PRGNAM/gpsman.tcl
chmod 0755 ./util/*.tcl

cp -a ./util/*.tcl $PKG/usr/lib/$PRGNAM
cp -a ./gmsrc/*.tcl $PKG/usr/lib/$PRGNAM
cp -a ./gmsrc/gmicons $PKG/usr/share/$PRGNAM
cp -a ./man/man1/* $PKG/usr/man/man1

( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $(find . -type l) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

ln -s /usr/share/$PRGNAM/gmicons $PKG/usr/lib/$PRGNAM/gmicons
ln -s /usr/lib/$PRGNAM/gpsman.tcl $PKG/usr/bin/gpsman
ln -s /usr/lib/$PRGNAM/dos2gpsman.tcl $PKG/usr/bin/dos2gpsman
ln -s /usr/lib/$PRGNAM/exerciser.tcl $PKG/usr/bin/exerciser
ln -s /usr/lib/$PRGNAM/gf2gmn.tcl $PKG/usr/bin/gf2gmn
ln -s /usr/lib/$PRGNAM/mb2gmn.tcl $PKG/usr/bin/mb2gmn
ln -s /usr/lib/$PRGNAM/mou2gmn.tcl $PKG/usr/bin/mou2gmn
ln -s /usr/lib/$PRGNAM/shape2quadr.tcl $PKG/usr/bin/shape2quadr
ln -s /usr/lib/$PRGNAM/wpsinfull.tcl $PKG/usr/bin/wpsinfull

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a LICENSE manual/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
